import { CartItem } from '@/store/modules/cart'
import http from '@/utils/request'

/**
 * 合并本地购物车
 * @param {Array<object>} cartList - 本地购物车数组
 * @param {String} item.skuId - 商品skuId
 * @param {Boolean} item.selected - 是否选中
 * @param {Integer} item.count - 数量
 */
export const reqMergeLocalCart = (cartList: Partial<CartItem>[]) => {
  return http('/member/cart/merge', 'post', cartList)
}

/**
 * 获取登录后的购物车列表
 * @returns Promise
 */
export const reqFindCartList = () => {
  return http<CartItem[]>('/member/cart', 'get')
}

/**
 * 加入购物车
 * @param {String} skuId - 商品SKUID
 * @param {Integer} count - 商品数量
 */
export const reqInsertCart = ({ skuId = '', count = 0 }) => {
  return http('/member/cart', 'post', { skuId, count })
}

/**
 * 删除商品（支持批量删除）
 * @param {Array<string>} ids - skuId集合
 * @returns Promise
 */
export const reqDeleteCart = (ids: string[]) => {
  return http('/member/cart', 'delete', { ids })
}

/**
 * 修改购物车商品的状态和数量
 * @param {String} goods.skuId - 商品sku (必须)
 * @param {Boolean} goods.selected - 选中状态 (可选)
 * @param {Integer} goods.count - 商品数量 (可选)
 */
export const reqUpdateCartSku = ({ skuId = '', ...goods }) => {
  return http(`/member/cart/${skuId}`, 'put', goods)
}

/**
 * 全选反选
 * @param {Boolean} selected - 选中状态
 * @param {Array<string>} ids - 有效商品skuId集合
 */
export const reqCheckAllCart = ({ selected, ids }: { selected: boolean, ids: string[] }) => {
  return http('/member/cart/selected', 'put', { selected, ids })
}
